#ifndef FUNCTION_H_
#define FUNCTION_H_

#include "sys_struct.h"

#define NO_FILE_HANDLE 0
#define NO_BLOCK_BUFFER 1
#define NO_INODE_BUFFER 2

#define RW_READ 0
#define RW_WRITE 1

#define ET_UP 0
#define ET_DOWN 1
#define ET_MODIFY 2

/*
 * One item stored in the directory.
 * i_no: the inode number of the item.
 * filename: the item name.
 * */
typedef struct file_entry{
	dword i_no;
	char filename[28];
}file_entry;

/**
 *
 * */
typedef struct path_info{
	int status;
	int fd_p;
	int ino_c;
	const char* p;
}path_info;

void fill_path_info(path_info *pi, int status, int fd_p, int ino_c, const char* p);

void list_append(data_memory *list, data_memory *item);
data_memory* list_remove(data_memory *list);
int list_empty(data_memory *list);

void fill_inode_disk(inode_disk* i1, inode_memory* i2);
void fill_inode_memory(inode_memory* i1, inode_disk* i2, int inode_no);

int readwrite(void* bf, dword block_no, int offset, int size, int rw);

int allocate_block();
int allocate_handle();
int allocate_inode();
int allocate_data(inode_memory *im);

int free_block(dword block_no);
void free_inode(int inode_no);
void free_data(inode_memory *im);

int create_inode();
void leave_system();

int get_phy_no(inode_memory *cur, int logic_no);

void write_inode(dword ino);

/*Given a inode number of disk, return a index of inode_buffer.*/
int load_inode(int i_no_disk);

int rw_inode(inode_disk* idisk, int n, int rw);
#endif
